//
//  STLogsModel.swift
//  SlowTrajectory
//
//  Created by iOS-Czz on 2023/10/23.
//

import UIKit

class STLogsViewModel{
    var dataSource = [STLogsModel]()
}
extension STLogsViewModel{
    func loadData(_ keyword:String, onSuccess: ((_ models: [STLogsModel]) ->Void)?){
        let dataHelper = STLogsModel.getUsingLKDBHelper()
        var sql = "select * from @t"
        if !keyword.isEmpty {
            sql = "select * from @t where log like '%\(keyword)%'"
        }
        let modelArr = dataHelper.search(withSQL: sql, to: STLogsModel.self) as! [STLogsModel]
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let sortedModels = modelArr.sorted { (model1, model2) -> Bool in
            guard let date1 = dateFormatter.date(from: model1.day),
                  let date2 = dateFormatter.date(from: model2.day) else {
                return false // 如果转换失败则直接返回 false
            }
            return date1 > date2
        }
        for model in sortedModels {
            model.weekday = model.day.dayOfWeekEnglish()!
        }
        self.dataSource = sortedModels
        onSuccess?(self.dataSource)
    }
    
}

@objcMembers
class STLogsModel: STBaseModel,Codable {
    var log = ""
    var idea = ""
    var day = ""
    var picture = ""
    var weekday = ""
    var isDefault = true
    override class func getTableName() -> String {
        return "STLogsModel"
    }
}
